#!/bin/sh
# -V <x.x.x.x>  -> specify runtime version number
# -m            -> generate map files under $ACCROOTFS/usr/map

if ! utils/check_build_env; then
    cd utils
    . ./build_env_init
    cd -
fi

if [ -e "${ACCPROJ}/runtime_ver" ]; then
    RUNTIME_VER=`< "${ACCPROJ}/runtime_ver"`
fi

while getopts "mV:" opts
do
    case "$opts" in
        V) RUNTIME_VER=$OPTARG ;;
        m) GEN_MAPFILE=y ;;
        *) ;;
    esac
done

if [ -z ${RUNTIME_VER} ]; then
    RUNTIME_VER=`date +%m.%d.%H.%M`
fi

echo "$RUNTIME_VER" > ${ACCROOTFS}/etc/runtime_ver


FAKEROOT_CMD=`which fakeroot`

# You can comment out the check below if you're sure
# it's okay to build without fakeroot
if [ -z ${FAKEROOT_CMD} ] ; then
        echo "Error! fakeroot is not installed. Please ask the administrator to install it."
        exit 1
fi

mkdir -p ${ACCPROJ}/runtime

# Somtimes files got from ClearCase snapshot view will see no execution permission set
# on shell script file. To avoid such problem, force setting execution permission
# here
chmod +x ${ACCROOTFS}/etc/rc
chmod +x ${ACCROOTFS}/etc/*.sh
find ${ACCROOTFS}/usr/bin -type f -exec chmod +x '{}' \;
find ${ACCROOTFS}/usr/sbin -type f -exec chmod +x '{}' \;
find ${ACCROOTFS}/lib -type f -exec chmod +x '{}' \;
find ${ACCROOTFS}/usr/lib -type f -exec chmod +x '{}' \;


cd ${ACCPROJ}/utils
if [ "x"$GEN_MAPFILE = "xy" ]; then
    echo "Generating map files on rootfs"
    ./do_sym.sh
    if [ $? != 0 ]; then
        echo "Error on generating map files"
        exit 1
    fi
fi

./do_strip.sh

echo "Making rootfs ..."
${FAKEROOT_CMD} /bin/bash ${ACCPROJ}/build_rootfs.sh
